package com.ruoyi.common.utils.hikvisionutils.model;
/**
 * ClassName: HikVisionAbilityUtils <br/>
 * Description: <br/>
 * date: 2021/1/4 10:46<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import com.alibaba.fastjson.JSON;
import com.hikvision.artemis.sdk.Response;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.ruoyi.common.utils.hikvisionutils.HikVisionHttpUtils;
import com.ruoyi.common.utils.hikvisionutils.HikVisionUtils;
import io.jsonwebtoken.Header;
import io.jsonwebtoken.Jwt;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: ruoyi
 * @description: 视频应用-视频能力相关api请求方法
 * @author: hesion
 * @create: 2021-01-04 10:46
 **/
public class HikVisionAbilityUtils {
    private static final Logger log = LoggerFactory.getLogger(HikVisionUtils.class);

    /**
     * 请根据自己的appKey和appSecret更换static静态块中的三个参数。
     */
    static {
        // artemis网关服务器ip端口
        ArtemisConfig.host = "116.9.120.142:9999";
        // 秘钥appkey
        ArtemisConfig.appKey = "26056044";
        // 秘钥appSecret
        ArtemisConfig.appSecret = "FGuZ8EHX6RjnyxLfnSEz";
    }

    /**
     * 能力开放平台的网站路径
     * 路径不用修改，就是/artemis
     */
    private static final String ARTEMIS_PATH = "/artemis";

    public static Response getPreviewURLs(Map<String, Object> params) throws InterruptedException {
        log.info("获取监控点预览取流URL");
        final String dataApi = ARTEMIS_PATH + "/api/video/v1/cameras/previewURLs";
        return doPostByMySelf(dataApi, params);
    }

    /**
     * @return com.hikvision.artemis.sdk.Response
     * @Author hst
     * @Description //TODO 通用请求方法
     * @Date 10:53 2021/1/4
     * @Param [dataApi, params]  dataApi请求地址，params请求参数
     **/
    private static Response doPostByMySelf(String dataApi, Map<String, Object> params) {
        try {
            Map<String, String> path = new HashMap<String, String>(2) {
                {
                    put("http://", dataApi);
                }
            };
            String body = JSON.toJSONString(params);

            Response response = HikVisionHttpUtils.doPostArtemis(path, body, null, null, "application/json");

            if(null==response){
                for (int i = 0; i < 2; i++) {
                    response=HikVisionHttpUtils.doPostArtemis(path, body, null, null, "application/json");
                    if (null != response){
                        break;
                    }
                }
                if (null==response){
                    response = new Response();
                    response.setStatusCode(500);
                    response.setErrorMessage("连接超时");
                    return response;
                }
            }
            return response;
        } catch (Exception e) {
            Response response = new Response();
            response.setStatusCode(200);
            response.setErrorMessage("请求超时");
            return response;
        }
    }

    public static Response getPlaybackURLs(Map<String, Object> params) {
        log.info("获取监控点回放取流URL");
        final String dataApi = ARTEMIS_PATH + "/api/video/v1/cameras/playbackURLs";
        return doPostByMySelf(dataApi, params);
    }

    public static Response getTalkURLs(Map<String, Object> params) {
        log.info("查询对讲URL");
        final String dataApi = ARTEMIS_PATH + "/api/video/v1/cameras/talkURLs";
        return doPostByMySelf(dataApi, params);
    }

    public static Response getControlling(Map<String, Object> params) {
        log.info("根据监控点编号进行云台操作");
        final String dataApi = ARTEMIS_PATH + "/api/video/v1/ptzs/controlling";
        return doPostByMySelf(dataApi, params);
    }

    public static Response doManualCapture(Map<String, Object> params) {
        log.info("手动抓图");
        final String dataApi = ARTEMIS_PATH + "/api/video/v1/manualCapture";
        return doPostByMySelf(dataApi, params);
    }

    public static Response doSelZoom(Map<String, Object> params) {
        log.info("监控点3D放大");
        final String dataApi = ARTEMIS_PATH + "/api/video/v1/ptzs/selZoom";
        return doPostByMySelf(dataApi, params);
    }

    public static Response doManualRecordStart(Map<String, Object> params) {
        log.info("开始手动录像");
        final String dataApi = ARTEMIS_PATH + "/api/video/v1/manualRecord/start";
        return doPostByMySelf(dataApi, params);
    }

    public static Response doManualRecordStop(Map<String, Object> params) {
        log.info("开始手动录像");
        final String dataApi = ARTEMIS_PATH + "/api/video/v1/manualRecord/stop";
        return doPostByMySelf(dataApi, params);
    }

    public static Response doManualRecordStatus(Map<String, Object> params) {
        log.info("获取手动录像状态");
        final String dataApi = ARTEMIS_PATH + "/api/video/v1/manualRecord/status";
        return doPostByMySelf(dataApi, params);
    }

    public static Response setPresetsAddition(Map<String, Object> params) {
        log.info("设置预置点信息");
        final String dataApi = ARTEMIS_PATH + "/api/video/v1/presets/addition";
        return doPostByMySelf(dataApi, params);
    }

    public static Response getPresetsSearches(Map<String, Object> params) {
        log.info("查询预置点信息");
        final String dataApi = ARTEMIS_PATH + "/api/video/v1/presets/searches";
        return doPostByMySelf(dataApi, params);
    }

    public static Response delPresetsDeletion(Map<String, Object> params) {
        log.info("删除预置点信息");
        final String dataApi = ARTEMIS_PATH + "/api/video/v1/presets/deletion";
        return doPostByMySelf(dataApi, params);
    }

    public static Response getPresets(Map<String, Object> params) {
        log.info("批量获取监控点的预置点信息");
        final String dataApi = ARTEMIS_PATH + "/api/video/v1/presets/get";
        return doPostByMySelf(dataApi, params);
    }

    public static Response getEventsPicture(Map<String, Object> params) {
        log.info("获取视频事件的图片");
        final String dataApi = ARTEMIS_PATH + "/api/video/v1/events/picture";
        return doPostByMySelf(dataApi, params);
    }

    public static Response getRecordList(Map<String, Object> params) {
        log.info("根据监控点列表查询录像完整性结果");
        final String dataApi = ARTEMIS_PATH + "/api/nms/v1/record/list";
        return doPostByMySelf(dataApi, params);
    }

    public static Response getVqdList(Map<String, Object> params) {
        log.info("根据监控点列表查询视频质量诊断结果");
        final String dataApi = ARTEMIS_PATH + "/api/nms/v1/vqd/list";
        return doPostByMySelf(dataApi, params);
    }

    public static Response getOnlineCameraGet(Map<String, Object> params) {
        log.info("获取监控点在线状态");
        final String dataApi = ARTEMIS_PATH + "/api/nms/v1/online/camera/get";
        return doPostByMySelf(dataApi, params);
    }

    public static Response getOnlineEncodeDeviceGet(Map<String, Object> params) {
        log.info("获取编码设备在线状态");
        final String dataApi = ARTEMIS_PATH + "/api/nms/v1/online/encode_device/get";
        return doPostByMySelf(dataApi, params);
    }
}
